/**
  ******************************************************************************
  * @file    ov2460.c
  * @brief   This file provides code for the configuration
  *          of all used GPIO pins.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "ov2640.h"
#include <stdio.h>
#include "ov2640cfg.h"

void OV2640_ChipReset(void)
{
  OV2640_Pwdn(0);
  vTaskDelay(10);
  
  OV2640_Reset(0);
  vTaskDelay(10);
  
  OV2640_Reset(1);
  vTaskDelay(10);
  
  SCCB_WriteReg(OV2640_DSP_RA_DLMT, 0x01);
  SCCB_WriteReg(OV2640_SENSOR_COM7, 0x80);
}

uint8_t OV2640_CheckChipID(void)
{
  uint16_t id;
  
	id = SCCB_ReadReg(OV2640_SENSOR_MIDH);
	id<<=8;
	id|=SCCB_ReadReg(OV2640_SENSOR_MIDL);
	SysDbgLog("MID:%d\r\n",id);
  if(id != OV2640_MID)
  {
    return 1;
  }

	id=SCCB_ReadReg(OV2640_SENSOR_PIDH);
	id<<=8;
	id|=SCCB_ReadReg(OV2640_SENSOR_PIDL);
  SysDbgLog("HID:%d\r\n",id);
  if(id != OV2640_PID)
  {
    return 2;
  }
  
	return 0;
}

void OV2640_BrightnessConfig(uint8_t Brightness)
{
  SCCB_WriteReg(0xff, 0x00);
  SCCB_WriteReg(0x7c, 0x00);
  SCCB_WriteReg(0x7d, 0x04);
  SCCB_WriteReg(0x7c, 0x09);
  SCCB_WriteReg(0x7d, Brightness);
  SCCB_WriteReg(0x7d, 0x00);
}

void OV2640_JPEG_Mode(void)
{
	uint16_t i=0;
  
	for(i=0;i<sizeof(ov2640_uxga_init_reg_tbl)/2;i++)
	{
	   	SCCB_WriteReg(ov2640_uxga_init_reg_tbl[i][0],ov2640_uxga_init_reg_tbl[i][1]);
 	}
  
	//ÉèÖÃ:YUV422¸ñÊ½
	for(i=0;i<(sizeof(ov2640_yuv422_reg_tbl)/2);i++)
	{
		SCCB_WriteReg(ov2640_yuv422_reg_tbl[i][0],ov2640_yuv422_reg_tbl[i][1]); 
	} 
	//ÉèÖÃ:Êä³öJPEGÊý¾Ý
	for(i=0;i<(sizeof(ov2640_jpeg_reg_tbl)/2);i++)
	{
		SCCB_WriteReg(ov2640_jpeg_reg_tbl[i][0],ov2640_jpeg_reg_tbl[i][1]);  
	}  
}

void OV2640_JPEGConfig(ImageFormat_TypeDef ImageFormat)
{
  uint32_t i;

  for(i=0; i<OV2640_JPEG_INIT_SIZE; i++)
  {
    SCCB_WriteReg(OV2640_JPEG_INIT[i][0], OV2640_JPEG_INIT[i][1]);
		vTaskDelay(1);
  }

  
  for(i=0; i<OV2640_YUV422_SIZE; i++)
  {
    SCCB_WriteReg(OV2640_YUV422[i][0], OV2640_YUV422[i][1]);
		vTaskDelay(1);
  }

  SCCB_WriteReg(0xff, 0x01);
  SCCB_WriteReg(0x15, 0x00);

 
  for(i=0; i<OV2640_JPEG_SIZE; i++)
  {
    SCCB_WriteReg(OV2640_JPEG[i][0], OV2640_JPEG[i][1]);
		vTaskDelay(1);
  }

  vTaskDelay(100);

  switch(ImageFormat)
  {
    case JPEG_160x120:
      for(i=0; i<OV2640_160x120_JPEG_SIZE; i++)
      {
        SCCB_WriteReg(OV2640_160x120_JPEG[i][0], OV2640_160x120_JPEG[i][1]);
        vTaskDelay(1);
      }
      break;

    case JPEG_176x144:
      for(i=0; i<OV2640_176x144_JPEG_SIZE; i++)
      {
        SCCB_WriteReg(OV2640_176x144_JPEG[i][0], OV2640_176x144_JPEG[i][1]);
      } 
      break;

    case JPEG_320x240:
      for(i=0; i<OV2640_320x240_JPEG_SIZE; i++)
			{
				SCCB_WriteReg(OV2640_320x240_JPEG[i][0], OV2640_320x240_JPEG[i][1]);
				vTaskDelay(1);
			}
      break;

    case JPEG_352x288:
      for(i=0; i<OV2640_352x288_JPEG_SIZE; i++)
      {
        SCCB_WriteReg(OV2640_352x288_JPEG[i][0], OV2640_352x288_JPEG[i][1]);
      }
      break;

    default:
      for(i=0; i<OV2640_160x120_JPEG_SIZE; i++)
      {
        SCCB_WriteReg(OV2640_160x120_JPEG[i][0], OV2640_160x120_JPEG[i][1]);
      }
      break;
  }
}

